<div class="breadcrumbs" id="breadcrumbs">
	<script type="text/javascript">
		try {
			ace.settings.check('breadcrumbs', 'fixed')
		} catch (e) {
		}
	</script>

	<ul class="breadcrumb">
		<li><i class="icon-home home-icon"></i> <a href="#">首页</a></li>
		<li><a href="#">用户管理</a></li>
		<li class="active">用户列表</li>
	</ul>
</div>
<div class="page-content">
	<div class="page-header">
		<h1>用户列表</h1>
	</div><!-- end /.page-header -->
	
	<div class="row">
		<div class="col-xs-14">
			<table id="grid-table"></table>
			<div id="grid-pager"></div>
			<script type="text/javascript">
				var $path_base="${basePath}" 	//this will be userd in gritter alerts containing images
			</script>
		</div><!-- end /.col -->
	</div><!-- end /.row -->
	<div id="rolesDialog"  title="分配角色" style="display: none">
		<ul id="roleTree" class="ztree"></ul>
	</div>
</div><!-- end /.page-content -->
<script type="text/javascript">
	jQuery(function($){
		var grid_selector = "#grid-table";
		var pager_selector = "#grid-pager";
		jQuery(grid_selector).jqGrid({
			url : '${basePath}/user/list',
			datatype: 'json',
			mytype : 'POST',
			height: 400,
			colNames:['编号','登录账号','密码','昵称','性别','Email','真实姓名','是否有效','改密码(仅修改时用)','操作','分配角色'],
			colModel:[
			          {name:'id',index:'id',width:30,sorttype:"int",editable:false},
			          {name:'account',index:'account',width:50,sorttype:"text",editable:true,editrules:{required:true}},
			          {name:'password',index:'password',hidden:true,sortable:false,width:50,
			        	  editable:true,editrules:{edithidden:true,required:true},viewable:false},
			          {name:'nickName',index:'nickName',sortable:false,width:50,editable:true,search:false},
			          {name:'sex',index:'sex',sortable:false,
			        	  formatter:function(cellvalue,options,rowObject){if(cellvalue==1){return "女";}if(cellvalue==0){return "男";}},
			        	  editable:true,edittype:"select",editoptions:{value:'0:男;1:女'},search:false,width:20},
			          {name:'email',index:'email',sortable:true,width:60,editable:true,editrules:{required:true, email:true},search:false},
			          {name:'userName',index:'userName',width:40,editable:true,search:true},
			          {name:'status',index:'status',sorttype:"int",
			        	  formatter:function(cellvalue,options,rowObject){if(cellvalue==1){return "是";}if(cellvalue==0){return "否";}},
			        	  edittype:"select",editoptions:{value:'1:是;0:否'},editable:true,search:false, width:40},
			          {name:'updatePassword',hidden:true,sortable:false,editable:true,edittype:'select',editoptions:{value:'0:否;1:是'},editrules:{edithidden:true},viewable:false},  
			          {name:'assign',width:'80',sortable:false,search:false,viewable:false,fixed:true,
			          	  formatter:assignFormat
			          },
			          {name:'myac',index:'',width:80, fixed:true, sortable:false,resize:false,
			        	  formatter:'actions',
			        	  formatoptions:{
			        		  keys:false,
			        		  delbutton:false,
							  editformbutton:true, editOptions:{recreateForm: true, beforeShowForm:beforeEditCallback,closeAfterEdit: true}
			        	  },
						  search:false,viewable:false}
					  
			          ],
			         	jsonReader:{repeatitems:false},
			          	viewrecords : true,
						rowNum:10,
						rowList:[10,20,30,50,100,1000],
						pager : pager_selector,
						altRows: true,
						loadtext:'正在加载数据...',
						multiselect: true,
				        multiboxonly: true,
				        loadComplete:function(){
				        	var table = this;
				        	setTimeout(function(){
								updatePagerIcons(table);
								enableTooltips(table);
				        	},0);
				        },
				        editurl:"${basePath}/user/cud",
				        caption:"用户列表",
				        autowidth:true
		});

		//引入jqgrid通用js 脚本
		<#include "/WEB-INF/common/_jqGrid.html"/>
		
		function assignFormat(cellvalue, options, rowObject){
			var id = rowObject['id'];
			return '<input class="btn btn-primary"  type="button" value="分配角色" onClick="assignRole('+id+')"/>'; 
		}
	});
	
	function zTreeOnCheck(event,treeId, treeNode){
		var treeObj = $.fn.zTree.getZTreeObj("roleTree");
		var nodes = treeObj.getChangeCheckedNodes();
		var roleIdAndChk = "";
		$.each(nodes,function(n,node){
			roleIdAndChk = roleIdAndChk + node.id + ":" + node.checked+",";
			node.checkedOld = node.checked;
		});
		var url = "${basePath}/user/updateRole";
		var uid = treeNode.uid;
		$.post(url,{uid:uid,roleIdAndChk:roleIdAndChk},
				function(text,status){
					if(text != ""){
						alert(text);
					}
		});
	}
	
	function assignRole(uid){
		var setting = {
				check:{
					enable:true,
					chkboxType:{"Y":"ps","N":"ps"}
				},
				data:{
					simpleData:{
						enable:true,
						idKey:"id",
						pidKey:"pId",
						rootPid:0
					}
				},
				callback:{
					onCheck:zTreeOnCheck
				}
		};
		
		$.ajax({
			async:true,
			cache:false,
			type:'POST',
			dataType:"json",
			data:{uid:uid},
			url:"${basePath}/user/getRoles",
			error:function(){
				alert('请求失败');
			},
			success:function(data){
				zTree = $.fn.zTree.init($("#roleTree"),setting,data);
				$("#rolesDialog").dialog({
					resizable:true,
					height: 400,
					modal:true,
					buttons:{
						"确定":function(){
							$(this).dialog("close");
						},
						"取消":function(){
							$(this).dialog("close");
						}
					},
				});
			}
		});
	}

</script>